#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

#include "PluginsManager/PluginsManager.h"

#include "interface/interfaceplugin.h"
#include "interface/DataInputInterface.h"
#include "interface/ShowInterface.h"

#include <QPluginLoader>
#include <QToolBar>
#include <QByteArray>
#include <QTime>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
	//this->setFixedWidth(1204);
	this->setMinimumWidth(1024);
	this->setMinimumHeight(600);
   m_toolBars = addToolBar(tr("ToolBar"));
    //on_pushButton_clicked();
   loadPluginsWidget();
   //ui->statusbar->setSizeGripEnabled(false);//去掉状态栏右下角的三角

   ui->statusbar->showMessage(tr("不二创新科技有限公司"), 2000);//显示临时信息2000ms 前面的正常信息被覆盖 当去掉后一项时，会一直显示

   QLabel *permanent = new QLabel(this);
   permanent->setFrameStyle(QFrame::Box | QFrame::Sunken);
   permanent->setText(tr("<a href=\"https://shop362552861.taobao.com/?spm=a230r.7195193.1997079397.76.6e6a56daOmi1hP\">不二创新科技有限公司</a>"));
   permanent->setOpenExternalLinks(true);//设置可以打开网站链接
   ui->statusbar->addPermanentWidget(permanent);//显示永久信息
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::recResult(QByteArray buf)
{
    qDebug()<<"rec:"<<QTime::currentTime()<<buf;
}




void MainWindow::loadPluginsWidget()
{
    //! 遍历所有插件
    foreach (QPluginLoader *loader, PluginsManager::getInstance().allPlugins()) {
       
        auto obj = loader->instance();
        qInfo()<<"Plugin Name:"<< PluginsManager::getInstance().getName(loader);
         InputDataInterface *input_plugin = qobject_cast<InputDataInterface*>(obj);
        if(input_plugin){    
            QWidget * widget = input_plugin->centerWidget();
            //!链接信号
            connect(loader->instance(),SIGNAL(sendResult(QByteArray)),
                    this,SLOT(recResult(QByteArray)));            
            m_toolBars->addWidget(widget);    
            continue;
        }
        ShowDataInterface *show_plugin = qobject_cast<ShowDataInterface*>(obj);
       if(show_plugin){    
           qDebug()<<"ShowDataInterface->show_plugin";
           QWidget * widget = show_plugin->toolbarWidget();
           QDockWidget * widget_info = show_plugin->dockWidgetInfo();
           //!链接信号
           connect(loader->instance(),SIGNAL(sendResult(QByteArray)),
                   this,SLOT(recResult(QByteArray)));   
           
           m_toolBars->addWidget(widget);    
           this->addDockWidget(Qt::RightDockWidgetArea,widget_info);
           this->setCentralWidget(show_plugin->centerWidget());
           continue;
       }
    }

    
}

void MainWindow::on_action_exit_destroyed()
{
    this->close();
}
